"""
文件相关的类定义
"""
import json

from my_package.data_define import ReCord


# 先设计一个抽象类，做顶层设计，确定有哪些功能需要实现

class FileReader:

    def read_data(self) -> list[ReCord]:
        """
        抽象方法：读取文件中的数据，将数据封装到ReCord对象中，并添加到list中返回即可
        :return: list
        """
        pass


class TextFileReader(FileReader):
    def __init__(self, path):
        self.path = path  # 文件路径

    # 复写（实现父类抽象的方法）
    def read_data(self) -> list[ReCord]:
        f = open(self.path, "r", encoding="UTF-8")
        record_list: list[ReCord] = []
        for line in f.readlines():
            # 消除\n
            line = line.strip()
            data_list = line.split(",")
            cord = ReCord(data_list[0], data_list[1], int(data_list[2]), data_list[3])
            record_list.append(cord)
        # 关闭文件
        f.close()
        return record_list


class JsonFileReader(FileReader):

    def __init__(self, path):
        self.path = path

    def read_data(self) -> list[ReCord]:
        """
        复写抽象类方法读取json数据的文件内容
        :return: record_list
        """
        f = open(self.path, "r", encoding="UTF-8")
        record_list: list[ReCord] = []
        for line in f.readlines():
            line = line.strip()
            data_dict = json.loads(line)
            cord = ReCord(data_dict["date"], data_dict["order_id"], int(data_dict["money"]), data_dict["province"])
            record_list.append(cord)
        return record_list


def read_file(fileReader: FileReader) -> list[ReCord]:
    return fileReader.read_data()


if __name__ == '__main__':
    text_file_path = "/Users/dongjian/Documents/学习/python-learn-资料/资料/第13章资料/2011年1月销售数据.txt"
    json_file_path = "/Users/dongjian/Documents/学习/python-learn-资料/资料/第13章资料/2011年2月销售数据JSON.txt"
    text_file_reader = TextFileReader(text_file_path)
    json_file_reader = JsonFileReader(json_file_path)
    list1 = text_file_reader.read_data()
    list2 = json_file_reader.read_data()

    for l1 in list1:
        print(f"list1-> {l1}")

    print("=====================================")

    for l2 in list2:
        print(f"list2-> {l2}")
